package com.qsmx.store.app.main.mem;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.AppCompatTextView;
import android.view.View;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.qsmx.store.app.R;
import com.qsmx.store.app.R2;
import com.qsmx.store.app.event.CardReadEvent;
import com.qsmx.store.app.event.TicketBeforeReadEvent;
import com.qsmx.store.app.event.TicketEndReadEvent;
import com.qsmx.store.app.fustion.FustionManager;
import com.qsmx.store.app.fustion.FustionTag;
import com.qsmx.store.app.http.HttpHelper;
import com.qsmx.store.app.http.HttpUrl;
import com.qsmx.store.app.main.mem.entity.MemManagerEntity;
import com.qsmx.store.app.util.CardReadUtil;
import com.qsmx.store.app.util.StringUtil;
import com.qsmx.store.core.delegates.AtmosDelegate;
import com.qsmx.store.core.fusion.FusionCode;
import com.qsmx.store.core.net.callback.IError;
import com.qsmx.store.core.net.callback.IFailure;
import com.qsmx.store.core.net.callback.ISuccess;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.lang.reflect.Type;
import java.util.WeakHashMap;

import butterknife.BindView;
import butterknife.OnClick;

public class MemCardPropertyTicketToOtherDelegate extends AtmosDelegate {

    private boolean isReadNow = false;
    private char mCardIcTag = 0x04;
    private MemManagerEntity memBeforeData;
    private MemManagerEntity memEndData;

    @BindView(R2.id.tv_ic_card_num)
    AppCompatTextView tvIcCardNum;
    @BindView(R2.id.tv_mem_card_mem_num)
    AppCompatTextView tvMemCardMemNum;
    @BindView(R2.id.tv_mem_start_before_ticket)
    AppCompatTextView tvMemStartBeforeTicket;
    @BindView(R2.id.tv_mem_start_to_ticket)
    AppCompatTextView tvMemStartToTicket;
    @BindView(R2.id.tv_mem_start_last_ticket)
    AppCompatTextView tvMemStartLastTicket;

    @BindView(R2.id.tv_ic_end_card_num)
    AppCompatTextView tvIcEndCardNum;
    @BindView(R2.id.tv_mem_card_end_mem_num)
    AppCompatTextView tvMemCardEndMemNum;
    @BindView(R2.id.tv_mem_end_before_ticket)
    AppCompatTextView tvMemEndBeforeTicket;
    @BindView(R2.id.tv_mem_end_to_ticket)
    AppCompatTextView tvMemEndToTicket;
    @BindView(R2.id.tv_mem_end_last_ticket)
    AppCompatTextView tvMemEndLastTicket;

    @BindView(R2.id.et_mem_remark)
    AppCompatEditText etMemRemark;

    @OnClick(R2.id.iv_card_scan)
    void onCardScan() {
        if (!isReadNow) {
            CardReadUtil.StartGetCardNum(getContext(), mCardIcTag, new TicketBeforeReadEvent(new Bundle()));
            isReadNow = true;
        }
    }

    @OnClick(R2.id.iv_end_card_scan)
    void onEndCardScan() {
        if (!isReadNow) {
            CardReadUtil.StartGetCardNum(getContext(), mCardIcTag, new TicketEndReadEvent(new Bundle()));
            isReadNow = true;
        }
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_mem_card_property_ticket_to_other;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {

    }

    private void onGetCardInfo(String cardNum) {
        WeakHashMap<String, Object> params = new WeakHashMap<>();
        params.put("deptId", FustionManager.mMachineData.getDeptId());
        params.put("icCardNo", cardNum);

        HttpHelper.RestClientGet(params, HttpUrl.MEM_CARD_INFO,
                new ISuccess() {
                    @SuppressLint("SetTextI18n")
                    @Override
                    public void onSuccess(int code, String response) {
                        Type type = new TypeToken<MemManagerEntity>() {
                        }.getType();
                        Gson gson = new Gson();

                    }
                }, new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        showShortToast(msg);
                    }
                }, new IFailure() {
                    @Override
                    public void onFailure() {

                    }
                });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(TicketBeforeReadEvent event) {
        if (event != null && event.getData() != null) {
            isReadNow = false;
            if (StringUtil.isNotEmpty(event.getData().getString("cardNum"))) {
                if (event.getData().getChar("tag") == mCardIcTag) {
                    onGetCardInfo(event.getData().getString("cardNum").substring(0, 18));
                }
            } else {
                showShortToast("这个卡是空的");
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(TicketEndReadEvent event) {
        if (event != null && event.getData() != null) {
            isReadNow = false;
            if (StringUtil.isNotEmpty(event.getData().getString("cardNum"))) {
                if (event.getData().getChar("tag") == mCardIcTag) {
                    onGetCardInfo(event.getData().getString("cardNum").substring(0, 18));
                }
            } else {
                showShortToast("这个卡是空的");
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        FusionCode.SCAN_TYPE = FusionCode.CANCEL;
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onStart() {
        super.onStart();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }
}
